
#include "Text.h"


Text::Text(string fontFile, string text, SDL_Color color, int ptsize = 10, int style = 0, int x = 0, int y = 0)
{
	this->fontFile = fontFile;
	this->text = text;
	this->ptsize = ptsize;
	this->style = style;
	this->color.r = color.r;
	this->color.g = color.g;
	this->color.b = color.b;
	this->box.x = x;
	this->box.y = y;

	this->font = TTF_OpenFont(this->fontFile.c_str(), this->ptsize);

	this->textSurface = TTF_RenderText_Solid(this->font, this->text.c_str(), this->color);

	this->box.h = this->textSurface->h;
	this->box.w = this->textSurface->w;
}


Text::~Text()
{
	TTF_CloseFont(font);
	SDL_FreeSurface(textSurface);
}


void Text::render(int cameraX, int cameraY)
{
	SDL_Rect dst;

	dst.x = box.x - cameraX;
	dst.y = box.y - cameraY;
	dst.h = 0;
	dst.w = 0;

	SDLBase::renderSurface(textSurface,&box,&dst);
}


void Text::setPos(int x, int y)
{
	box.x = x;
	box.y = y;
}


void Text::setText(string text)
{
	SDL_FreeSurface(textSurface);
	this->text = text;
	this->textSurface = TTF_RenderText_Solid(this->font, this->text.c_str(), this->color);

	this->box.h = this->textSurface->h;
	this->box.w = this->textSurface->w;
}


void Text::setStyle(int style)
{
	SDL_FreeSurface(textSurface);
	this->style = style;
	TTF_SetFontStyle(font, this->style);
	this->textSurface = TTF_RenderText_Solid(this->font, this->text.c_str(), this->color);

	this->box.h = this->textSurface->h;
	this->box.w = this->textSurface->w;
}


void Text::setColor(SDL_Color color)
{
	SDL_FreeSurface(textSurface);
	this->color.r = color.r;
	this->color.g = color.g;
	this->color.b = color.b;
	this->textSurface = TTF_RenderText_Solid(this->font, this->text.c_str(), this->color);

	this->box.h = this->textSurface->h;
	this->box.w = this->textSurface->w;
}


void Text::setSize(int ptsize)
{
	SDL_FreeSurface(textSurface);
	this->ptsize = ptsize;
	this->font = TTF_OpenFont(this->fontFile.c_str(), this->ptsize);
	this->textSurface = TTF_RenderText_Solid(this->font, this->text.c_str(), this->color);

	this->box.h = this->textSurface->h;
	this->box.w = this->textSurface->w;
}



